package com.ruoyi.framework.config;

import com.ruoyi.framework.config.properties.EsProperties;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

@Configuration
public class EsConfig {
    @Autowired
    private EsProperties esProperties;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        String[] urls = esProperties.getUrls();
        HttpHost[] hosts = new HttpHost[urls.length];
        for (int i = 0; i < urls.length; i++) {
            String host = urls[i].split(":")[0];
            String port = urls[i].split(":")[1];
            hosts[i] = new HttpHost(host, Integer.parseInt(port), "http");
        }
        RestClientBuilder builder = RestClient.builder(hosts)
                .setRequestConfigCallback(builder1 -> {
                    builder1.setConnectTimeout(esProperties.getConnTimeout());
                    builder1.setSocketTimeout(esProperties.getSocketTimeout());
                    builder1.setConnectionRequestTimeout(esProperties.getConnectionRequestTimeout());
                    return builder1;
                });
        //保活策略
//        RestClientBuilder builder = RestClient.builder(new HttpHost("172.22.0.134", 9200, "http"));
        builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                .setDefaultIOReactorConfig(IOReactorConfig.custom()
                        .setSoKeepAlive(true)
                        .build()));
        // 设置认证信息
        if (StringUtils.isNotEmpty(esProperties.getUsername()) && StringUtils.isNotEmpty(esProperties.getPassword())) {
            //如果没配置密码就可以不用下面这两部
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esProperties.getUsername(), esProperties.getPassword()));
            builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
                httpAsyncClientBuilder.disableAuthCaching();
                return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            });
        }
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
//    @Bean
//    public ElasticsearchRestTemplate elasticsearchTemplate() {
//        return new ElasticsearchRestTemplate(restHighLevelClient());
//    }

}
